<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-if="isChannel" v-model="dataForm.cid" class="filter-item" clearable>
        <el-option
          v-for="item in channelList"
          :key="item.cid"
          :label="item.name + ' (' + item.cid + ')'"
          :value="item.cid"
        />
      </el-select>
      <el-button v-if="isChannel" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button v-if="checkPermission(permissionRoles.recharge_config_download)" v-waves :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
      <el-upload
        v-if="checkPermission(permissionRoles.recharge_config_upload)"
        ref="upload"
        class="filter-item"
        action="api/gm/config/recharge/upload"
        :headers="header"
        :on-success="successInfo"
        :before-upload="beforeUpload"
        :file-list="fileList"
        :data="dataForm"
      >
        <el-button type="success" icon="el-icon-upload2">上传</el-button>
      </el-upload>
      <el-button v-if="isSelect" class="filter-item" type="danger" @click="deleteSelection()">删除所选</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="740" @selection-change="handleSelectionChange">
      <el-table-column v-if="isChannel" type="selection" width="55"></el-table-column>
      <el-table-column v-if="isChannel" label="渠道" prop="cid" align="center"></el-table-column>
      <el-table-column label="商品ID" prop="id" align="center"></el-table-column>
      <el-table-column label="金额" prop="amount" align="center"></el-table-column>
      <el-table-column label="描述" prop="describe" align="center"></el-table-column>
    </el-table>
    <pagination v-show="total>0" :limit.sync="pageSize" :total="total" />
  </div>
</template>

<script>
import waves from '@/directive/waves' // 实现点击波浪纹
import checkPermission from '@/utils/permission'
import Pagination from '@/components/Pagination'
import { getRechargeConfig, deleteRechargeConfig } from '@/api/config'
import { getAllChannel } from '@/api/ops'
export default {
  name: 'BehaviorLog',
  components: { Pagination },
  directives: { waves },
  props: {
    listType: { type: Number, default: 0 }
  },
  data() {
    return {
      multipleSelection: [],
      permissionRoles: this.$store.state.user.permissionRoles,
      header: {
        Authorization: this.$store.state.user.token
      },
      list: [],
      total: 0,
      pageSize: 1000,
      listLoading: false,
      downloadLoading: false,
      fileList: [],
      channelList: [],
      dataForm: {
        cid: '',
        listType: this.listType
      },
      cdataForm: {
        cid: ''
      }
    }
  },
  computed: {
    isChannel() {
      return this.listType === 1
    },
    isSelect() {
      return this.multipleSelection.length > 0
    }
  },
  created() {
    this.init()
  },
  methods: {
    beforeUpload(file) {
      // if (this.isChannel && this.dataForm.cid === '') {
      //   this.$message.error('请选择渠道')
      //   return false
      // }
      return true
    },
    checkPermission,
    init() {
      this.getList()
      this.getAllChannel()
    },
    handleFilter() {
      // if (this.isChannel && this.dataForm.cid === '') {
      //   this.$message.error('请选择渠道')
      //   return false
      // }
      this.getList()
    },
    async getList() {
      // if (this.isChannel && this.dataForm.cid === '') {
      //   return
      // }
      this.listLoading = true
      console.log(this.dataForm)
      const res = await getRechargeConfig(this.dataForm)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
      this.cdataForm.cid = res.data.cid
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    async getAllChannel() {
      const res = await getAllChannel()
      this.channelList = res.data
    },
    handleDownload() {
      /* if (this.isChannel && this.dataForm.cid === '') {
        this.$message.error('请选择渠道')
        return false
      } */
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        let tHeader, filterVal
        if (this.isChannel) {
          tHeader = ['渠道', '档位ID', '金额', '档位描述']
          filterVal = ['cid', 'id', 'amount', 'describe']
        } else {
          tHeader = ['档位ID', '金额', '档位描述']
          filterVal = ['id', 'amount', 'describe']
        }
        const data = this.formatJson(filterVal)
        const filename = this.cdataForm.cid + '充值档位配置'
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: filename
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    successInfo(res, file, fileList) {
      this.init()
      if (res.code === 40003) {
        this.$message.error(res.message)
      } else {
        this.$refs.upload.clearFiles()
        this.visible = false
        this.$message.success(res.message)
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteSelection() {
      this.$confirm('确认删除所选？')
        .then(() => {
          console.log(this.multipleSelection)
          const ids = this.multipleSelection.map(item => ({ id: item.id, cid: item.cid }))
          console.log(ids)
          deleteRechargeConfig({ ids }).then(res => {
            const msg = '删除成功,总共' + res['data']['sum'] + '个,成功删除 ' + res['data']['success'] + '个'
            this.$message.success(msg)
            this.multipleSelection = []
            this.getList()
          }).catch(() => {
            this.$message.error('删除失败')
          })
        })
        .catch(() => {
          this.$message.info('已取消')
        })
    }
  }
}
</script>
